package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对密码进行md5加密,然后再比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }


        //3、返回实体对象
        return employee;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     */
    @Override
    public void insertEmp(EmployeeDTO employeeDTO) {
        //从ThreadLocal提供的存储空间中获取创建者id
        Long createUser= BaseContext.getCurrentId();


        Employee emp=Employee.builder()
                .password(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()))
                .status(StatusConstant.ENABLE)
                .build();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,emp);

        employeeMapper.insertEmp(emp);
    }

    /**
     * 分页查询员工
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult getEmps(String name, String page, String pageSize) {

        //1. 设置分页参数
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(pageSize));

        //2.  执行查询
        Page<Employee> p = employeeMapper.getEmps(name);

        //3.  封装PageVo对象，包含查询的总记录数和查询结果
        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());

        return pageResult;
    }

    /**
     * 修改员工账号状态
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        employeeMapper.changeStatus(status,id);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employee queryById(Long id) {
        Employee employee = employeeMapper.queryById(id);
        return employee;
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     */
    @Override
    public void changeEmp(EmployeeDTO employeeDTO) {

        Long UpdateUser= BaseContext.getCurrentId();


        Employee emp=Employee.builder()
                .build();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,emp);

        employeeMapper.changeEmp(emp);
    }

}
